using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    class DesktopViewModel
    {
        public int Id
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }
    }

    public class DesktopController : Controller
    {
        //
        // GET: /Desktop/
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();

        public ActionResult Index()
        {
            var desktops = _db.DESKTOP.Where(c => c.LaSanPhamCustom == false);

            foreach (var item in desktops)
            {
                item.SAN_PHAMReference.Load();
                item.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                item.SAN_PHAM.NHA_PHAN_PHOIReference.Load();
                item.SAN_PHAM.NHA_SAN_XUATReference.Load();
                item.SAN_PHAM.HINH_ANH.Load();
            }

            return View(desktops.ToList());
        }

        //
        // GET: /Desktop/Details/5
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Details(int id)
        {
            try
            {
                var desktop = _db.DESKTOP.Where(c => c.id == id).First();

                desktop.CPUReference.Load();
                desktop.RAMReference.Load();
                desktop.VGAReference.Load();
                desktop.HDDReference.Load();
                desktop.MAINBOARDReference.Load();
                desktop.HE_DIEU_HANHReference.Load();
                desktop.SOUND_CARDReference.Load();
                desktop.CD_DVDReference.Load();
                desktop.MONITORReference.Load();
                desktop.SPEAKERReference.Load();
                desktop.CASEReference.Load();
                desktop.POWERReference.Load();
                desktop.KEYBOARDReference.Load();
                desktop.MOUSEReference.Load();

                desktop.CPU.SAN_PHAMReference.Load();
                desktop.RAM.SAN_PHAMReference.Load();
                desktop.VGA.SAN_PHAMReference.Load();
                desktop.HDD.SAN_PHAMReference.Load();
                desktop.MAINBOARD.SAN_PHAMReference.Load();
                desktop.HE_DIEU_HANH.SAN_PHAMReference.Load();
                desktop.SOUND_CARD.SAN_PHAMReference.Load();
                desktop.CD_DVD.SAN_PHAMReference.Load();
                desktop.MONITOR.SAN_PHAMReference.Load();
                desktop.SPEAKER.SAN_PHAMReference.Load();
                desktop.CASE.SAN_PHAMReference.Load();
                desktop.POWER.SAN_PHAMReference.Load();
                desktop.KEYBOARD.SAN_PHAMReference.Load();
                desktop.MOUSE.SAN_PHAMReference.Load();

                desktop.CPU.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.RAM.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.VGA.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.HDD.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.MAINBOARD.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.HE_DIEU_HANH.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.SOUND_CARD.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.CD_DVD.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.MONITOR.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.SPEAKER.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.CASE.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.POWER.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.KEYBOARD.SAN_PHAM.LOAI_SAN_PHAMReference.Load();
                desktop.MOUSE.SAN_PHAM.LOAI_SAN_PHAMReference.Load();

                desktop.SAN_PHAMReference.Load();
                desktop.SAN_PHAM.LOAI_SAN_PHAMReference.Load();

                ViewData["SAN_PHAM"] = desktop.SAN_PHAM;
                ViewData["GOP_Y"] = _db.GOP_Y.Where((c) => c.SAN_PHAM.id == id);
                ViewData["DANH_GIA"] = _db.DANH_GIA.Where((c) => c.SAN_PHAM.id == id);

                return View(desktop);
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee | SiteRoles.Member)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Details(FormCollection form)
        {
            try
            {
                int idSanPham = int.Parse(form.Get("idSanPham"));
                int SoLuong = int.Parse(form.Get("SoLuong"));

                return RedirectToAction("AddToCart", "Cart", new { idSanPham, SoLuong });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Desktop/Create
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee | SiteRoles.Member)]
        public ActionResult Create()
        {
            try
            {
                List<DesktopViewModel> cpuList = new List<DesktopViewModel>();

                foreach (var cpu in _db.CPU)
                {
                    cpu.SAN_PHAMReference.Load();
                    cpu.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    cpuList.Add(new DesktopViewModel
                    {
                        Id = cpu.id,
                        Value = cpu.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + cpu.SAN_PHAM.TenSanPham + " - " + cpu.Socket + ", " + cpu.Speed + ", " + cpu.Cache + ", " + cpu.Bus + " ( " + cpu.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CPU"] = new SelectList(cpuList, "Id", "Value");

                List<DesktopViewModel> ramList = new List<DesktopViewModel>();

                foreach (var ram in _db.RAM)
                {
                    ram.SAN_PHAMReference.Load();
                    ram.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    ramList.Add(new DesktopViewModel
                    {
                        Id = ram.id,
                        Value = ram.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + ram.SAN_PHAM.TenSanPham + " - " + ram.DungLuong + ", " + ram.Bus + " ( " + ram.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["RAM"] = new SelectList(ramList, "Id", "Value");

                List<DesktopViewModel> vgaList = new List<DesktopViewModel>();

                foreach (var vga in _db.VGA)
                {
                    vga.SAN_PHAMReference.Load();
                    vga.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    vgaList.Add(new DesktopViewModel
                    {
                        Id = vga.id,
                        Value = vga.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + vga.SAN_PHAM.TenSanPham + " - " + vga.VideoMemory + ", " + vga.Engine + ", " + vga.InterfaceMemory + ", " + vga.CongGiaoTiep + " ( " + vga.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["VGA"] = new SelectList(vgaList, "Id", "Value");


                List<DesktopViewModel> hddList = new List<DesktopViewModel>();

                foreach (var hdd in _db.HDD)
                {
                    hdd.SAN_PHAMReference.Load();
                    hdd.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    hddList.Add(new DesktopViewModel
                    {
                        Id = hdd.id,
                        Value = (hdd.SAN_PHAM.NHA_SAN_XUAT != null) ? hdd.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null + hdd.SAN_PHAM.TenSanPham + " - " + hdd.DungLuong + ", " + hdd.Chuan + ", " + hdd.SoVong + " ( " + hdd.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["HDD"] = new SelectList(hddList, "Id", "Value");

                List<DesktopViewModel> mbList = new List<DesktopViewModel>();

                foreach (var mb in _db.MAINBOARD)
                {
                    mb.SAN_PHAMReference.Load();
                    mb.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    mbList.Add(new DesktopViewModel
                    {
                        Id = mb.id,
                        Value = ((mb.SAN_PHAM.NHA_SAN_XUAT != null) ? mb.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + mb.SAN_PHAM.TenSanPham + " - " + mb.Cpu + ", " + mb.Chipset + ", " + mb.Ram + ", " + mb.Video + ", " + mb.Audio + ", " + mb.io_control + ", " + mb.Interface + ", " + mb.Network + ", " + mb.form_factor + ", " + mb.Expansion + " ( " + mb.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MAINBOARD"] = new SelectList(mbList, "Id", "Value");

                List<DesktopViewModel> scList = new List<DesktopViewModel>();

                foreach (var sc in _db.SOUND_CARD)
                {
                    sc.SAN_PHAMReference.Load();
                    sc.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    scList.Add(new DesktopViewModel
                    {
                        Id = sc.id,
                        Value = ((sc.SAN_PHAM.NHA_SAN_XUAT != null) ? sc.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + sc.SAN_PHAM.TenSanPham + " - " + sc.CongSuat + ", " + sc.Ex + " ( " + sc.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["SOUND_CARD"] = new SelectList(scList, "Id", "Value");

                List<DesktopViewModel> cdList = new List<DesktopViewModel>();

                foreach (var cd in _db.CD_DVD)
                {
                    cd.SAN_PHAMReference.Load();
                    cd.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    cdList.Add(new DesktopViewModel
                    {
                        Id = cd.id,
                        Value = ((cd.SAN_PHAM.NHA_SAN_XUAT != null) ? cd.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + cd.SAN_PHAM.TenSanPham + " - " + cd.TocDo + ", " + cd.CongGiaoTiep + " ( " + cd.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CD_DVD"] = new SelectList(cdList, "Id", "Value");

                List<DesktopViewModel> monitorList = new List<DesktopViewModel>();

                foreach (var monitor in _db.MONITOR)
                {
                    monitor.SAN_PHAMReference.Load();
                    monitor.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    monitorList.Add(new DesktopViewModel
                    {
                        Id = monitor.id,
                        Value = ((monitor.SAN_PHAM.NHA_SAN_XUAT != null) ? monitor.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + monitor.SAN_PHAM.TenSanPham + " - " + monitor.DoTuongPhan + ", " + monitor.ThoiGianDapUng + ", " + monitor.CongGiaoTiep + ", " + monitor.ex + " ( " + monitor.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MONITOR"] = new SelectList(monitorList, "Id", "Value");

                List<DesktopViewModel> speakerList = new List<DesktopViewModel>();

                foreach (var speaker in _db.SPEAKER)
                {
                    speaker.SAN_PHAMReference.Load();
                    speaker.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    speakerList.Add(new DesktopViewModel
                    {
                        Id = speaker.id,
                        Value = ((speaker.SAN_PHAM.NHA_SAN_XUAT != null) ? speaker.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + speaker.SAN_PHAM.TenSanPham + " - " + speaker.CongSuat + ", " + speaker.Chuan + " ( " + speaker.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["SPEAKER"] = new SelectList(speakerList, "Id", "Value");

                List<DesktopViewModel> casList = new List<DesktopViewModel>();

                foreach (var cas in _db.CASE)
                {
                    cas.SAN_PHAMReference.Load();
                    cas.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    casList.Add(new DesktopViewModel
                    {
                        Id = cas.id,
                        Value = ((cas.SAN_PHAM.NHA_SAN_XUAT != null) ? cas.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + cas.SAN_PHAM.TenSanPham + " - " + cas.KhongGianChua + ", " + cas.KichThuoc + ", " + cas.Nguon + ", " + cas.MoRong + ", " + cas.CongGiaoTiep + ", " + cas.HoTro + ", " + cas.TanNhiet + ", " + cas.VatLieu + " ( " + cas.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CASE"] = new SelectList(casList, "Id", "Value");

                List<DesktopViewModel> powerList = new List<DesktopViewModel>();

                foreach (var power in _db.POWER)
                {
                    power.SAN_PHAMReference.Load();
                    power.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    powerList.Add(new DesktopViewModel
                    {
                        Id = power.id,
                        Value = ((power.SAN_PHAM.NHA_SAN_XUAT != null) ? power.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + power.SAN_PHAM.TenSanPham + " - " + power.CongSuat + ", " + power.Quat + ", " + power.Connector + " ( " + power.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["POWER"] = new SelectList(powerList, "Id", "Value");

                List<DesktopViewModel> hdhList = new List<DesktopViewModel>();

                foreach (var hdh in _db.HE_DIEU_HANH)
                {
                    hdh.SAN_PHAMReference.Load();
                    hdh.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    hdhList.Add(new DesktopViewModel
                    {
                        Id = hdh.id,
                        Value = ((hdh.SAN_PHAM.NHA_SAN_XUAT != null) ? hdh.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + hdh.SAN_PHAM.TenSanPham + " - " + hdh.Ten + " ( " + hdh.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["HDH"] = new SelectList(hdhList, "Id", "Value");

                List<DesktopViewModel> kbList = new List<DesktopViewModel>();

                foreach (var kb in _db.KEYBOARD)
                {
                    kb.SAN_PHAMReference.Load();
                    kb.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    kbList.Add(new DesktopViewModel
                    {
                        Id = kb.id,
                        Value = ((kb.SAN_PHAM.NHA_SAN_XUAT != null) ? kb.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + kb.SAN_PHAM.TenSanPham + " - " + kb.Mau + " - " + kb.CongGiaoTiep + " ( " + kb.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["KEYBOARD"] = new SelectList(kbList, "Id", "Value");


                List<DesktopViewModel> mouseList = new List<DesktopViewModel>();

                foreach (var mouse in _db.MOUSE)
                {
                    mouse.SAN_PHAMReference.Load();
                    mouse.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    mouseList.Add(new DesktopViewModel
                    {
                        Id = mouse.id,
                        Value = ((mouse.SAN_PHAM.NHA_SAN_XUAT != null) ? mouse.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + mouse.SAN_PHAM.TenSanPham + " - " + mouse.Mau + " - " + mouse.CongGiaoTiep + " ( " + mouse.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MOUSE"] = new SelectList(mouseList, "Id", "Value");

                return View();
            }
            catch
            {
                return RedirectToAction("Index");
            }
        } 

        //
        // POST: /Desktop/Create
        [SpiderE3Authorize(Roles = SiteRoles.Admin | SiteRoles.Employee | SiteRoles.Member)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                int idCpu = int.Parse(form.Get("idCpu"));
                int idRam = int.Parse(form.Get("idRam"));
                int idVga = int.Parse(form.Get("idVga"));
                int idHdd = int.Parse(form.Get("idHdd"));
                int idMainBoard = int.Parse(form.Get("idMainBoard"));
                int idHdh = int.Parse(form.Get("idHdh"));
                int idSoundCard = int.Parse(form.Get("idSoundCard"));
                int idCdDvd = int.Parse(form.Get("idCdDvd"));
                int idMonitor = int.Parse(form.Get("idMonitor"));
                int idSpeaker = int.Parse(form.Get("idSpeaker"));
                int idCase = int.Parse(form.Get("idCase"));
                int idPower = int.Parse(form.Get("idPower"));
                int idKeyboard = int.Parse(form.Get("idKeyboard"));
                int idMouse = int.Parse(form.Get("idMouse"));

                DESKTOP desktop = new DESKTOP {
                    CPU = _db.CPU.Where(c => c.id == idCpu).First(),
                    RAM = _db.RAM.Where(c => c.id == idRam).First(),
                    VGA = _db.VGA.Where(c => c.id == idVga).First(),
                    HDD = _db.HDD.Where(c => c.id == idHdd).First(),
                    MAINBOARD = _db.MAINBOARD.Where(c => c.id == idMainBoard).First(),
                    HE_DIEU_HANH = _db.HE_DIEU_HANH.Where(c => c.id == idHdh).First(),
                    SOUND_CARD = _db.SOUND_CARD.Where(c => c.id == idSoundCard).First(),
                    CD_DVD = _db.CD_DVD.Where(c => c.id == idCdDvd).First(),
                    MONITOR = _db.MONITOR.Where(c => c.id == idMonitor).First(),
                    SPEAKER = _db.SPEAKER.Where(c => c.id == idSpeaker).First(),
                    CASE = _db.CASE.Where(c => c.id == idCase).First(),
                    POWER = _db.POWER.Where(c => c.id == idPower).First(),
                    KEYBOARD = _db.KEYBOARD.Where(c => c.id == idKeyboard).First(),
                    MOUSE = _db.MOUSE.Where(c => c.id == idMouse).First()
                };

                desktop.CPU.SAN_PHAMReference.Load();
                desktop.RAM.SAN_PHAMReference.Load();
                desktop.VGA.SAN_PHAMReference.Load();
                desktop.HDD.SAN_PHAMReference.Load();
                desktop.MAINBOARD.SAN_PHAMReference.Load();
                desktop.HE_DIEU_HANH.SAN_PHAMReference.Load();
                desktop.SOUND_CARD.SAN_PHAMReference.Load();
                desktop.CD_DVD.SAN_PHAMReference.Load();
                desktop.MONITOR.SAN_PHAMReference.Load();
                desktop.SPEAKER.SAN_PHAMReference.Load();
                desktop.CASE.SAN_PHAMReference.Load();
                desktop.POWER.SAN_PHAMReference.Load();
                desktop.KEYBOARD.SAN_PHAMReference.Load();
                desktop.MOUSE.SAN_PHAMReference.Load();

                long TongTien = 0;

                TongTien += desktop.CPU.SAN_PHAM.Gia;
                TongTien += desktop.RAM.SAN_PHAM.Gia;
                TongTien += desktop.VGA.SAN_PHAM.Gia;
                TongTien += desktop.HDD.SAN_PHAM.Gia;
                TongTien += desktop.MAINBOARD.SAN_PHAM.Gia;
                TongTien += desktop.HE_DIEU_HANH.SAN_PHAM.Gia;
                TongTien += desktop.SOUND_CARD.SAN_PHAM.Gia;
                TongTien += desktop.CD_DVD.SAN_PHAM.Gia;
                TongTien += desktop.MONITOR.SAN_PHAM.Gia;
                TongTien += desktop.SPEAKER.SAN_PHAM.Gia;
                TongTien += desktop.CASE.SAN_PHAM.Gia;
                TongTien += desktop.POWER.SAN_PHAM.Gia;
                TongTien += desktop.KEYBOARD.SAN_PHAM.Gia;
                TongTien += desktop.MOUSE.SAN_PHAM.Gia;

                SAN_PHAM sp = new SAN_PHAM
                {
                    LOAI_SAN_PHAM = _db.LOAI_SAN_PHAM.Where(c => c.TenLoaiSanPham == "DESKTOP").First(),
                    NgayThemSP = DateTime.Now,
                    TenSanPham = "Customed Desktop",
                    MoTa = "Customed Destop",
                    BaoHanh = "1 Nam",
                    Gia = TongTien,
                    Diem = 0
                };

                SiteRoles role = SpiderE3AuthorizeAttribute.GetCurrentRole(HttpContext);
                if (role == SiteRoles.Member)
                {
                    desktop.LaSanPhamCustom = true;
                }
                else if (role == SiteRoles.Employee || role == SiteRoles.Admin)
                {
                    desktop.LaSanPhamCustom = false;
                }

                _db.AddToSAN_PHAM(sp);
                desktop.SAN_PHAM = sp;
                _db.AddToDESKTOP(desktop);
                _db.SaveChanges();

                return RedirectToAction("Details", new { id = desktop.id });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Desktop/Edit/5
 
        public ActionResult Edit(int id)
        {
            try
            {
                var desktop = _db.DESKTOP.Where(c => c.id == id).First();

                SiteRoles role = SpiderE3AuthorizeAttribute.GetCurrentRole(HttpContext);
                if (role == SiteRoles.Member && desktop.LaSanPhamCustom == false)
                {
                    return RedirectToAction("Index");
                }

                List<DesktopViewModel> cpuList = new List<DesktopViewModel>();

                foreach (var cpu in _db.CPU)
                {
                    cpu.SAN_PHAMReference.Load();
                    cpu.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    cpuList.Add(new DesktopViewModel
                    {
                        Id = cpu.id,
                        Value = cpu.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + cpu.SAN_PHAM.TenSanPham + " - " + cpu.Socket + ", " + cpu.Speed + ", " + cpu.Cache + ", " + cpu.Bus + " ( " + cpu.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CPU"] = new SelectList(cpuList, "Id", "Value", desktop.CPU.id);

                List<DesktopViewModel> ramList = new List<DesktopViewModel>();

                foreach (var ram in _db.RAM)
                {
                    ram.SAN_PHAMReference.Load();
                    ram.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    ramList.Add(new DesktopViewModel
                    {
                        Id = ram.id,
                        Value = ram.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + ram.SAN_PHAM.TenSanPham + " - " + ram.DungLuong + ", " + ram.Bus + " ( " + ram.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["RAM"] = new SelectList(ramList, "Id", "Value", desktop.RAM.id);

                List<DesktopViewModel> vgaList = new List<DesktopViewModel>();

                foreach (var vga in _db.VGA)
                {
                    vga.SAN_PHAMReference.Load();
                    vga.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    vgaList.Add(new DesktopViewModel
                    {
                        Id = vga.id,
                        Value = vga.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " + vga.SAN_PHAM.TenSanPham + " - " + vga.VideoMemory + ", " + vga.Engine + ", " + vga.InterfaceMemory + ", " + vga.CongGiaoTiep + " ( " + vga.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["VGA"] = new SelectList(vgaList, "Id", "Value", desktop.VGA.id);


                List<DesktopViewModel> hddList = new List<DesktopViewModel>();

                foreach (var hdd in _db.HDD)
                {
                    hdd.SAN_PHAMReference.Load();
                    hdd.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    hddList.Add(new DesktopViewModel
                    {
                        Id = hdd.id,
                        Value = (hdd.SAN_PHAM.NHA_SAN_XUAT != null) ? hdd.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null + hdd.SAN_PHAM.TenSanPham + " - " + hdd.DungLuong + ", " + hdd.Chuan + ", " + hdd.SoVong + " ( " + hdd.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["HDD"] = new SelectList(hddList, "Id", "Value", desktop.HDD.id);

                List<DesktopViewModel> mbList = new List<DesktopViewModel>();

                foreach (var mb in _db.MAINBOARD)
                {
                    mb.SAN_PHAMReference.Load();
                    mb.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    mbList.Add(new DesktopViewModel
                    {
                        Id = mb.id,
                        Value = ((mb.SAN_PHAM.NHA_SAN_XUAT != null) ? mb.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + mb.SAN_PHAM.TenSanPham + " - " + mb.Cpu + ", " + mb.Chipset + ", " + mb.Ram + ", " + mb.Video + ", " + mb.Audio + ", " + mb.io_control + ", " + mb.Interface + ", " + mb.Network + ", " + mb.form_factor + ", " + mb.Expansion + " ( " + mb.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MAINBOARD"] = new SelectList(mbList, "Id", "Value", desktop.MAINBOARD.id);

                List<DesktopViewModel> scList = new List<DesktopViewModel>();

                foreach (var sc in _db.SOUND_CARD)
                {
                    sc.SAN_PHAMReference.Load();
                    sc.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    scList.Add(new DesktopViewModel
                    {
                        Id = sc.id,
                        Value = ((sc.SAN_PHAM.NHA_SAN_XUAT != null) ? sc.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + sc.SAN_PHAM.TenSanPham + " - " + sc.CongSuat + ", " + sc.Ex + " ( " + sc.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["SOUND_CARD"] = new SelectList(scList, "Id", "Value", desktop.SOUND_CARD.id);

                List<DesktopViewModel> cdList = new List<DesktopViewModel>();

                foreach (var cd in _db.CD_DVD)
                {
                    cd.SAN_PHAMReference.Load();
                    cd.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    cdList.Add(new DesktopViewModel
                    {
                        Id = cd.id,
                        Value = ((cd.SAN_PHAM.NHA_SAN_XUAT != null) ? cd.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + cd.SAN_PHAM.TenSanPham + " - " + cd.TocDo + ", " + cd.CongGiaoTiep + " ( " + cd.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CD_DVD"] = new SelectList(cdList, "Id", "Value", desktop.CD_DVD.id);

                List<DesktopViewModel> monitorList = new List<DesktopViewModel>();

                foreach (var monitor in _db.MONITOR)
                {
                    monitor.SAN_PHAMReference.Load();
                    monitor.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    monitorList.Add(new DesktopViewModel
                    {
                        Id = monitor.id,
                        Value = ((monitor.SAN_PHAM.NHA_SAN_XUAT != null) ? monitor.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + monitor.SAN_PHAM.TenSanPham + " - " + monitor.DoTuongPhan + ", " + monitor.ThoiGianDapUng + ", " + monitor.CongGiaoTiep + ", " + monitor.ex + " ( " + monitor.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MONITOR"] = new SelectList(monitorList, "Id", "Value", desktop.MONITOR.id);

                List<DesktopViewModel> speakerList = new List<DesktopViewModel>();

                foreach (var speaker in _db.SPEAKER)
                {
                    speaker.SAN_PHAMReference.Load();
                    speaker.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    speakerList.Add(new DesktopViewModel
                    {
                        Id = speaker.id,
                        Value = ((speaker.SAN_PHAM.NHA_SAN_XUAT != null) ? speaker.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + speaker.SAN_PHAM.TenSanPham + " - " + speaker.CongSuat + ", " + speaker.Chuan + " ( " + speaker.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["SPEAKER"] = new SelectList(speakerList, "Id", "Value", desktop.SPEAKER.id);

                List<DesktopViewModel> casList = new List<DesktopViewModel>();

                foreach (var cas in _db.CASE)
                {
                    cas.SAN_PHAMReference.Load();
                    cas.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    casList.Add(new DesktopViewModel
                    {
                        Id = cas.id,
                        Value = ((cas.SAN_PHAM.NHA_SAN_XUAT != null) ? cas.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + cas.SAN_PHAM.TenSanPham + " - " + cas.KhongGianChua + ", " + cas.KichThuoc + ", " + cas.Nguon + ", " + cas.MoRong + ", " + cas.CongGiaoTiep + ", " + cas.HoTro + ", " + cas.TanNhiet + ", " + cas.VatLieu + " ( " + cas.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["CASE"] = new SelectList(casList, "Id", "Value", desktop.CASE.id);

                List<DesktopViewModel> powerList = new List<DesktopViewModel>();

                foreach (var power in _db.POWER)
                {
                    power.SAN_PHAMReference.Load();
                    power.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    powerList.Add(new DesktopViewModel
                    {
                        Id = power.id,
                        Value = ((power.SAN_PHAM.NHA_SAN_XUAT != null) ? power.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + power.SAN_PHAM.TenSanPham + " - " + power.CongSuat + ", " + power.Quat + ", " + power.Connector + " ( " + power.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["POWER"] = new SelectList(powerList, "Id", "Value", desktop.POWER.id);

                List<DesktopViewModel> hdhList = new List<DesktopViewModel>();

                foreach (var hdh in _db.HE_DIEU_HANH)
                {
                    hdh.SAN_PHAMReference.Load();
                    hdh.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    hdhList.Add(new DesktopViewModel
                    {
                        Id = hdh.id,
                        Value = ((hdh.SAN_PHAM.NHA_SAN_XUAT != null) ? hdh.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + hdh.SAN_PHAM.TenSanPham + " - " + hdh.Ten + " ( " + hdh.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["HDH"] = new SelectList(hdhList, "Id", "Value", desktop.HE_DIEU_HANH.id);

                List<DesktopViewModel> kbList = new List<DesktopViewModel>();

                foreach (var kb in _db.KEYBOARD)
                {
                    kb.SAN_PHAMReference.Load();
                    kb.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    kbList.Add(new DesktopViewModel
                    {
                        Id = kb.id,
                        Value = ((kb.SAN_PHAM.NHA_SAN_XUAT != null) ? kb.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + kb.SAN_PHAM.TenSanPham + " - " + kb.Mau + " - " + kb.CongGiaoTiep + " ( " + kb.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["KEYBOARD"] = new SelectList(kbList, "Id", "Value", desktop.KEYBOARD.id);


                List<DesktopViewModel> mouseList = new List<DesktopViewModel>();

                foreach (var mouse in _db.MOUSE)
                {
                    mouse.SAN_PHAMReference.Load();
                    mouse.SAN_PHAM.NHA_SAN_XUATReference.Load();

                    mouseList.Add(new DesktopViewModel
                    {
                        Id = mouse.id,
                        Value = ((mouse.SAN_PHAM.NHA_SAN_XUAT != null) ? mouse.SAN_PHAM.NHA_SAN_XUAT.TenNSX + " - " : null) + mouse.SAN_PHAM.TenSanPham + " - " + mouse.Mau + " - " + mouse.CongGiaoTiep + " ( " + mouse.SAN_PHAM.Gia.ToString("000,000,000") + " VND )."
                    });
                }

                ViewData["MOUSE"] = new SelectList(mouseList, "Id", "Value", desktop.MOUSE.id);

                return View();
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Desktop/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                int idCpu = int.Parse(form.Get("idCpu"));
                int idRam = int.Parse(form.Get("idRam"));
                int idVga = int.Parse(form.Get("idVga"));
                int idHdd = int.Parse(form.Get("idHdd"));
                int idMainBoard = int.Parse(form.Get("idMainBoard"));
                int idHdh = int.Parse(form.Get("idHdh"));
                int idSoundCard = int.Parse(form.Get("idSoundCard"));
                int idCdDvd = int.Parse(form.Get("idCdDvd"));
                int idMonitor = int.Parse(form.Get("idMonitor"));
                int idSpeaker = int.Parse(form.Get("idSpeaker"));
                int idCase = int.Parse(form.Get("idCase"));
                int idPower = int.Parse(form.Get("idPower"));
                int idKeyboard = int.Parse(form.Get("idKeyboard"));
                int idMouse = int.Parse(form.Get("idMouse"));

                DESKTOP desktop = _db.DESKTOP.Where(c => c.id == id).First();

                desktop.CPU = _db.CPU.Where(c => c.id == idCpu).First();
                desktop.RAM = _db.RAM.Where(c => c.id == idRam).First();
                desktop.VGA = _db.VGA.Where(c => c.id == idVga).First();
                desktop.HDD = _db.HDD.Where(c => c.id == idHdd).First();
                desktop.MAINBOARD = _db.MAINBOARD.Where(c => c.id == idMainBoard).First();
                desktop.HE_DIEU_HANH = _db.HE_DIEU_HANH.Where(c => c.id == idHdh).First();
                desktop.SOUND_CARD = _db.SOUND_CARD.Where(c => c.id == idSoundCard).First();
                desktop.CD_DVD = _db.CD_DVD.Where(c => c.id == idCdDvd).First();
                desktop.MONITOR = _db.MONITOR.Where(c => c.id == idMonitor).First();
                desktop.SPEAKER = _db.SPEAKER.Where(c => c.id == idSpeaker).First();
                desktop.CASE = _db.CASE.Where(c => c.id == idCase).First();
                desktop.POWER = _db.POWER.Where(c => c.id == idPower).First();
                desktop.KEYBOARD = _db.KEYBOARD.Where(c => c.id == idKeyboard).First();
                desktop.MOUSE = _db.MOUSE.Where(c => c.id == idMouse).First();

                desktop.SAN_PHAMReference.Load();
                desktop.CPU.SAN_PHAMReference.Load();
                desktop.RAM.SAN_PHAMReference.Load();
                desktop.VGA.SAN_PHAMReference.Load();
                desktop.HDD.SAN_PHAMReference.Load();
                desktop.MAINBOARD.SAN_PHAMReference.Load();
                desktop.HE_DIEU_HANH.SAN_PHAMReference.Load();
                desktop.SOUND_CARD.SAN_PHAMReference.Load();
                desktop.CD_DVD.SAN_PHAMReference.Load();
                desktop.MONITOR.SAN_PHAMReference.Load();
                desktop.SPEAKER.SAN_PHAMReference.Load();
                desktop.CASE.SAN_PHAMReference.Load();
                desktop.POWER.SAN_PHAMReference.Load();
                desktop.KEYBOARD.SAN_PHAMReference.Load();
                desktop.MOUSE.SAN_PHAMReference.Load();

                long TongTien = 0;

                TongTien += desktop.CPU.SAN_PHAM.Gia;
                TongTien += desktop.RAM.SAN_PHAM.Gia;
                TongTien += desktop.VGA.SAN_PHAM.Gia;
                TongTien += desktop.HDD.SAN_PHAM.Gia;
                TongTien += desktop.MAINBOARD.SAN_PHAM.Gia;
                TongTien += desktop.HE_DIEU_HANH.SAN_PHAM.Gia;
                TongTien += desktop.SOUND_CARD.SAN_PHAM.Gia;
                TongTien += desktop.CD_DVD.SAN_PHAM.Gia;
                TongTien += desktop.MONITOR.SAN_PHAM.Gia;
                TongTien += desktop.SPEAKER.SAN_PHAM.Gia;
                TongTien += desktop.CASE.SAN_PHAM.Gia;
                TongTien += desktop.POWER.SAN_PHAM.Gia;
                TongTien += desktop.KEYBOARD.SAN_PHAM.Gia;
                TongTien += desktop.MOUSE.SAN_PHAM.Gia;

                desktop.SAN_PHAM.Gia = TongTien;

                var ctgiohangs = _db.CHI_TIET_GIO_HANG.Where(c => c.SAN_PHAM.id == desktop.SAN_PHAM.id);

                foreach (var ctgiohang in ctgiohangs)
                {
                    ctgiohang.TongTien = ctgiohang.SoLuong * ctgiohang.SAN_PHAM.Gia;
                }

                //SAN_PHAM sp = new SAN_PHAM { LOAI_SAN_PHAM = _db.LOAI_SAN_PHAM.Where(c => c.TenLoaiSanPham == "DESKTOP").First(), NgayThemSP = DateTime.Now, TenSanPham = "Customed Desktop", MoTa = "Customed Destop", BaoHanh = "1 Nam", Gia = TongTien };
                //desktop.LaSanPhamCustom = true;

                //_db.AddToDESKTOP(desktop);
                _db.SaveChanges();

                return RedirectToAction("Details", new { id = desktop.id });
            }
            catch
            {
                return View();
            }
        }
    }
}
